﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace FinWeb3.Models.Repository
{
    public class GroupRepository : IRepository<Group>
    {
        public void Save(Group item, IIdentity user)
        {
            if (item.Id == 0)
            {
                item.UserName = user.Name;
                item.Save();
            }
            else
            {
                var group = Get(item.Id, user);

                if (group != null)
                {
                    group.Category = item.Category;
                    group.Day = item.Day;
                    group.Description = item.Description;
                    group.FirstDay = item.FirstDay;
                    group.HasDetailList = item.HasDetailList;
                    group.LastDay = item.LastDay;
                    group.UserName = user.Name;
                    group.Value = item.Value;
                    group.ReceiveBill = item.ReceiveBill;

                    group.Save();
                }
            }
        }

        public void Delete(Group item, IIdentity user)
        {
            var group = Get(item.Id, user);

            if (group != null)
                group.Delete();
        }

        public Group Get(int id, IIdentity user)
        {
            return Group.All().Where(x => x.UserName == user.Name && x.Id == id).FirstOrDefault();
        }

        public IQueryable<Group> GetAll(IIdentity user)
        {
            return Group.All().Where(x => x.UserName == user.Name);
        }
    }
}
